

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=&#34;auto&#34;>



<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/img/favicon.png">
  <link rel="icon" type="image/png" href="/img/favicon.png">
  <meta name="viewport"
        content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="description" content="CodeHope">
  <meta name="author" content="CodeHope">
  <meta name="keywords" content="希望">
  <title>Python-WTF表单扩展 - CodeHope</title>

  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.5.3/dist/css/bootstrap.min.css" />


  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/github-markdown-css@4.0.0/github-markdown.min.css" />
  <link  rel="stylesheet" href="/lib/hint/hint.min.css" />

  
    
    
      
      
        
          
          
          
        
        <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/prismjs@1.22.0/themes/prism-tomorrow.min.css" />
      
      
        <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/prismjs@1.22.0/plugins/line-numbers/prism-line-numbers.min.css" />
      
    
  

  
    <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3.5.7/dist/jquery.fancybox.min.css" />
  



<!-- 主题依赖的图标库，不要自行修改 -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_ba1fz6golrf.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_kmeydafke9r.css">


<link  rel="stylesheet" href="/css/main.css" />

<!-- 自定义样式保持在最底部 -->


  <script id="fluid-configs">
    var Fluid = window.Fluid || {};
    var CONFIG = {"hostname":"quancundexiwang.wang","root":"/","version":"1.8.7","typing":{"enable":true,"typeSpeed":70,"cursorChar":"_","loop":false},"anchorjs":{"enable":true,"element":"h1,h2,h3,h4,h5,h6","placement":"left","visible":"hover","icon":""},"progressbar":{"enable":true,"height_px":3,"color":"#29d","options":{"showSpinner":true,"trickleSpeed":100}},"copy_btn":true,"image_zoom":{"enable":true},"toc":{"enable":true,"headingSelector":"h1,h2,h3,h4,h5,h6","collapseDepth":0},"lazyload":{"enable":true,"onlypost":true},"web_analytics":{"enable":false,"baidu":null,"google":null,"gtag":null,"tencent":{"sid":null,"cid":null},"woyaola":null,"cnzz":null,"leancloud":{"app_id":"Mi65hxq7VAFUDwOLeIGAOgiV-gzGzoHsz","app_key":"hMuhiD4FRqhns4giqLiEH9HG","server_url":null}}};
  </script>
  <script  src="/js/utils.js" ></script>
  <script  src="/js/color-schema.js" ></script>
<meta name="generator" content="Hexo 5.3.0"></head>


<body>
  <header style="height: 80vh;">
    <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand"
       href="/">&nbsp;<strong>CodeHope</strong>&nbsp;</a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/">
                <i class="iconfont icon-home-fill"></i>
                村头
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/archives/">
                <i class="iconfont icon-archive-fill"></i>
                归档
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/categories/">
                <i class="iconfont icon-category-fill"></i>
                分类
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/tags/">
                <i class="iconfont icon-tags-fill"></i>
                标签
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/about/">
                <i class="iconfont icon-user-fill"></i>
                我
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" data-toggle="modal" data-target="#modalSearch">&nbsp;<i
                class="iconfont icon-search"></i>&nbsp;</a>
          </li>
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" href="javascript:">&nbsp;<i
                class="iconfont icon-dark" id="color-toggle-icon"></i>&nbsp;</a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

    <div class="banner" id="banner" parallax=true
         style="background: url('https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=351499770,1971798553&fm=26&gp=0.jpg') repeat center center;
           background-size: cover;">
      <div class="full-bg-img">
        <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
          <div class="page-header text-center fade-in-up">
            <span class="h2" id="subtitle" title="Python-WTF表单扩展">
              
            </span>

            
              <div class="mt-3">
  
  
    <span class="post-meta">
      <i class="iconfont icon-date-fill" aria-hidden="true"></i>
      <time datetime="2020-12-15 17:39" pubdate>
        2020年12月15日 下午
      </time>
    </span>
  
</div>

<div class="mt-1">
  
    
    <span class="post-meta mr-2">
      <i class="iconfont icon-chart"></i>
      1.6k 字
    </span>
  

  
    
    <span class="post-meta mr-2">
      <i class="iconfont icon-clock-fill"></i>
      
      
      25
       分钟
    </span>
  

  
  
</div>

            
          </div>

          
            <div class="scroll-down-bar">
              <i class="iconfont icon-arrowdown"></i>
            </div>
          
        </div>
      </div>
    </div>
  </header>

  <main>
    
      

<div class="container-fluid nopadding-x">
  <div class="row nomargin-x">
    <div class="d-none d-lg-block col-lg-2"></div>
    <div class="col-lg-8 nopadding-x-md">
      <div class="container nopadding-x-md" id="board-ctn">
        <div class="py-5" id="board">
          <article class="post-content mx-auto">
            <!-- SEO header -->
            <h1 style="display: none">Python-WTF表单扩展</h1>
            
            <div class="markdown-body">
              <h1 id="FLASK-WTF-for-FORM"><a href="#FLASK-WTF-for-FORM" class="headerlink" title="FLASK-WTF for FORM"></a><code>FLASK-WTF</code> for <code>FORM</code></h1><ul>
<li><p>集成了 <code>wtforms</code></p>
</li>
<li><p>CSRF 保护</p>
</li>
<li><p>与<code>Flask-Uploads</code>一起支持文件上传</p>
</li>
</ul>
<h2 id="1-FLASK-WTF安装使用"><a href="#1-FLASK-WTF安装使用" class="headerlink" title="1.FLASK-WTF安装使用"></a>1.FLASK-WTF安装使用</h2><h3 id="1-1-安装配置"><a href="#1-1-安装配置" class="headerlink" title="1.1 安装配置"></a>1.1 安装配置</h3><p><span style='color:red'>安装</span></p>
<div class="code-wrapper"><pre class="line-numbers language-shell" data-language="shell"><code class="language-shell">pip install flask-wtf<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre></div>
<p><span style='color:red'>配置<code>(WTF_CSRF_SECRET_KEY=&#39;random string&#39;)</code></span></p>
<div class="code-wrapper"><pre class="line-numbers language-python" data-language="python"><code class="language-python">app<span class="token punctuation">.</span>config<span class="token punctuation">[</span>wtf_csrf_secret_key<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token string">'ChangHao~!@#$%'</span>  <span class="token comment"># wtf配置`csrf`密钥</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre></div>
<h3 id="1-2-第一个表单模型"><a href="#1-2-第一个表单模型" class="headerlink" title="1.2 第一个表单模型"></a>1.2 第一个表单模型</h3><p> <code>form.py</code></p>
<div class="code-wrapper"><pre class="line-numbers language-python" data-language="python"><code class="language-python"><span class="token keyword">from</span> flask_wtf <span class="token keyword">import</span> FlaskForm  <span class="token comment"># 引入表单模型的类</span>
<span class="token keyword">from</span> wtforms <span class="token keyword">import</span> StringField<span class="token punctuation">,</span> PasswordField<span class="token punctuation">,</span> SubmitField


<span class="token keyword">class</span> <span class="token class-name">LoginForm</span><span class="token punctuation">(</span>FlaskForm<span class="token punctuation">)</span><span class="token punctuation">:</span>
    username <span class="token operator">=</span> StringField<span class="token punctuation">(</span>label<span class="token operator">=</span><span class="token string">'用户名'</span><span class="token punctuation">)</span>
    password <span class="token operator">=</span> PasswordField<span class="token punctuation">(</span>label<span class="token operator">=</span><span class="token string">'密码'</span><span class="token punctuation">)</span>
    submit <span class="token operator">=</span> SubmitField<span class="token punctuation">(</span>label<span class="token operator">=</span><span class="token string">'提交'</span><span class="token punctuation">)</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div>
<p><code>app.py</code></p>
<ul>
<li> <code>LoginForm()</code> 实例化自定义的表单对象</li>
<li><code>request.form.get(&#39;key&#39;)</code> 获取<code>表单的值</code></li>
<li><code>login_form.username.data</code> 通过实例form对象获取值</li>
</ul>
<div class="code-wrapper"><pre class="line-numbers language-python" data-language="python"><code class="language-python"><span class="token decorator annotation punctuation">@app<span class="token punctuation">.</span>route</span><span class="token punctuation">(</span><span class="token string">'/form_test'</span><span class="token punctuation">,</span> methods<span class="token operator">=</span><span class="token punctuation">[</span><span class="token string">'GET'</span><span class="token punctuation">,</span> <span class="token string">'POST'</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
<span class="token keyword">def</span> <span class="token function">form_test</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
    login_form <span class="token operator">=</span> LoginForm<span class="token punctuation">(</span><span class="token punctuation">)</span>
    <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">'username1'</span><span class="token punctuation">,</span> login_form<span class="token punctuation">.</span>username<span class="token punctuation">.</span>data<span class="token punctuation">)</span>
    <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">'username2'</span><span class="token punctuation">,</span> request<span class="token punctuation">.</span>form<span class="token punctuation">.</span>get<span class="token punctuation">(</span><span class="token string">'username'</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
    <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">'password'</span><span class="token punctuation">,</span> request<span class="token punctuation">.</span>form<span class="token punctuation">.</span>get<span class="token punctuation">(</span><span class="token string">'password'</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
    <span class="token keyword">return</span> render_template<span class="token punctuation">(</span><span class="token string">'login.html'</span><span class="token punctuation">,</span> login_form<span class="token operator">=</span>login_form<span class="token punctuation">)</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div>
<p><code>login.html</code></p>
<div class="code-wrapper"><pre class="line-numbers language-html" data-language="html"><code class="language-html"><span class="token doctype"><span class="token punctuation">&lt;!</span><span class="token doctype-tag">DOCTYPE</span> <span class="token name">html</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>html</span> <span class="token attr-name">lang</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>en<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>head</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>meta</span> <span class="token attr-name">charset</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>UTF-8<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>title</span><span class="token punctuation">></span></span>Title<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>title</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>head</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>body</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>form</span> <span class="token attr-name">action</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>/form_test<span class="token punctuation">"</span></span> <span class="token attr-name">method</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>post<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>p</span><span class="token punctuation">></span></span>
        &#123;&#123; login_form.username.label &#125;&#125;:
        &#123;&#123; login_form.username &#125;&#125;
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>p</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>p</span><span class="token punctuation">></span></span>
        &#123;&#123; login_form.password.label &#125;&#125;:
        &#123;&#123; login_form.password &#125;&#125;
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>p</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>p</span><span class="token punctuation">></span></span>
        &#123;&#123; login_form.submit.label &#125;&#125;:
        &#123;&#123; login_form.submit &#125;&#125;
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>p</span><span class="token punctuation">></span></span>

<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>form</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>body</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>html</span><span class="token punctuation">></span></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div>
<p><img src="http://qiniuyun.quancundexiwang.wang/20201214235437.png" srcset="/img/loading.gif" alt="image-20201214235437479"></p>
<p><img src="http://qiniuyun.quancundexiwang.wang/20201215102956.png" srcset="/img/loading.gif" alt="image-20201215102956053"></p>
<p><img src="http://qiniuyun.quancundexiwang.wang/20201215104846.png" srcset="/img/loading.gif" alt="image-20201215104846267"></p>
<p>顺利获取到表单的值啦！</p>
<p><code>注册</code> <code>app.py</code></p>
<div class="code-wrapper"><pre class="line-numbers language-python" data-language="python"><code class="language-python">
<span class="token decorator annotation punctuation">@app<span class="token punctuation">.</span>route</span><span class="token punctuation">(</span><span class="token string">'/register'</span><span class="token punctuation">,</span> methods<span class="token operator">=</span><span class="token punctuation">[</span><span class="token string">'GET'</span><span class="token punctuation">,</span> <span class="token string">'POST'</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
<span class="token keyword">def</span> <span class="token function">form_test</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
    register_form <span class="token operator">=</span> RegisterForm<span class="token punctuation">(</span>csrf_enabled<span class="token operator">=</span><span class="token boolean">False</span><span class="token punctuation">)</span>
    <span class="token keyword">try</span><span class="token punctuation">:</span>
        <span class="token keyword">if</span> register_form<span class="token punctuation">.</span>validate_on_submit<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
            username <span class="token operator">=</span> register_form<span class="token punctuation">.</span>username<span class="token punctuation">.</span>data
            email <span class="token operator">=</span> register_form<span class="token punctuation">.</span>email<span class="token punctuation">.</span>data
            password <span class="token operator">=</span> register_form<span class="token punctuation">.</span>password<span class="token punctuation">.</span>data
            personal_status <span class="token operator">=</span> register_form<span class="token punctuation">.</span>personal_status<span class="token punctuation">.</span>data
            user <span class="token operator">=</span> User<span class="token punctuation">(</span>username<span class="token punctuation">,</span> password<span class="token punctuation">,</span> email<span class="token punctuation">,</span> personal_status<span class="token punctuation">)</span>
            db<span class="token punctuation">.</span>session<span class="token punctuation">.</span>add<span class="token punctuation">(</span>user<span class="token punctuation">)</span>
            db<span class="token punctuation">.</span>session<span class="token punctuation">.</span>commit<span class="token punctuation">(</span><span class="token punctuation">)</span>
        <span class="token keyword">else</span><span class="token punctuation">:</span>
            <span class="token keyword">print</span><span class="token punctuation">(</span>register_form<span class="token punctuation">.</span>errors<span class="token punctuation">)</span>
    <span class="token keyword">except</span> IOError <span class="token keyword">as</span> E<span class="token punctuation">:</span>
        <span class="token keyword">return</span> make_response<span class="token punctuation">(</span><span class="token punctuation">&#123;</span>
            <span class="token string">'msg'</span><span class="token punctuation">:</span> E
        <span class="token punctuation">&#125;</span><span class="token punctuation">)</span>
    <span class="token keyword">return</span> render_template<span class="token punctuation">(</span><span class="token string">'login.html'</span><span class="token punctuation">,</span> register_form<span class="token operator">=</span>register_form<span class="token punctuation">)</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div>
<p><code>form.py</code></p>
<div class="code-wrapper"><pre class="line-numbers language-python" data-language="python"><code class="language-python"><span class="token keyword">from</span> flask_wtf <span class="token keyword">import</span> FlaskForm  <span class="token comment"># 引入表单模型的类</span>
<span class="token keyword">from</span> wtforms <span class="token keyword">import</span> StringField<span class="token punctuation">,</span> PasswordField<span class="token punctuation">,</span> SubmitField<span class="token punctuation">,</span> SelectField
<span class="token keyword">from</span> wtforms<span class="token punctuation">.</span>validators <span class="token keyword">import</span> DataRequired<span class="token punctuation">,</span> Length


<span class="token keyword">class</span> <span class="token class-name">RegisterForm</span><span class="token punctuation">(</span>FlaskForm<span class="token punctuation">)</span><span class="token punctuation">:</span>
    username <span class="token operator">=</span> StringField<span class="token punctuation">(</span>label<span class="token operator">=</span><span class="token string">'用户名'</span><span class="token punctuation">,</span> validators<span class="token operator">=</span><span class="token punctuation">[</span>DataRequired<span class="token punctuation">(</span>message<span class="token operator">=</span><span class="token string">'请填写用户名'</span><span class="token punctuation">)</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
    email <span class="token operator">=</span> StringField<span class="token punctuation">(</span>label<span class="token operator">=</span><span class="token string">'邮箱'</span><span class="token punctuation">,</span> validators<span class="token operator">=</span><span class="token punctuation">[</span>DataRequired<span class="token punctuation">(</span><span class="token string">'请填写邮箱地址'</span><span class="token punctuation">)</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
    password <span class="token operator">=</span> PasswordField<span class="token punctuation">(</span>label<span class="token operator">=</span><span class="token string">'密码'</span><span class="token punctuation">,</span> validators<span class="token operator">=</span><span class="token punctuation">[</span>DataRequired<span class="token punctuation">(</span><span class="token string">'请填写密码'</span><span class="token punctuation">)</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
    personal_status <span class="token operator">=</span> SelectField<span class="token punctuation">(</span>label<span class="token operator">=</span><span class="token string">'婚姻情况'</span><span class="token punctuation">,</span> validators<span class="token operator">=</span><span class="token punctuation">[</span>DataRequired<span class="token punctuation">(</span><span class="token string">'请选择婚姻状况'</span><span class="token punctuation">)</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
                                  choices<span class="token operator">=</span><span class="token punctuation">[</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token string">'已婚'</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token string">'未婚'</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">,</span> <span class="token string">'单身'</span><span class="token punctuation">)</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
    submit <span class="token operator">=</span> SubmitField<span class="token punctuation">(</span>label<span class="token operator">=</span><span class="token string">'提交'</span><span class="token punctuation">)</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div>
<p><code>login.html</code></p>
<div class="code-wrapper"><pre class="line-numbers language-jinja2" data-language="jinja2"><code class="language-jinja2">&lt;form action&#x3D;&quot;&#123;&#123; url_for(&#39;form_test&#39;) &#125;&#125;&quot; method&#x3D;&quot;post&quot;&gt;
    &lt;p&gt;
        &#123;&#123; register_form.username.label &#125;&#125;:
        &#123;&#123; register_form.username &#125;&#125;
    &lt;&#x2F;p&gt;
    &lt;p&gt;
        &#123;&#123; register_form.email.label &#125;&#125;:
        &#123;&#123; register_form.email &#125;&#125;
    &lt;&#x2F;p&gt;
    &lt;p&gt;
        &#123;&#123; register_form.password.label &#125;&#125;:
        &#123;&#123; register_form.password &#125;&#125;
    &lt;&#x2F;p&gt;
     &lt;p&gt;
        &#123;&#123; register_form.personal_status.label &#125;&#125;:
        &#123;&#123; register_form.personal_status &#125;&#125;
    &lt;&#x2F;p&gt;
    &lt;p&gt;
        &#123;&#123; register_form.submit.label &#125;&#125;:
        &#123;&#123; register_form.submit &#125;&#125;
    &lt;&#x2F;p&gt;

&lt;&#x2F;form&gt;<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div>
<p><img src="http://qiniuyun.quancundexiwang.wang/20201215123406.png" srcset="/img/loading.gif" alt="image-20201215123406347"> </p>
<p>查看数据库</p>
<!DOCTYPE html>
<html>
  <head>
    <title></title>
    <meta charset="UTF-8">
  </head>
<body>
<table border="1" style="border-collapse:collapse">
<tr>
  <th>id</th>
  <th>username</th>
  <th>password</th>
  <th>email</th>
  <th>person_status</th>
</tr>
<tr>
  <td>1</td>
  <td>changhao</td>
  <td>123</td>
  <td>1933310737@qq.com</td>
  <td>0</td>
</tr>
<tr>
  <td>8</td>
  <td>shamiao5201314</td>
  <td>11223344</td>
  <td>i933310737@qq.com</td>
  <td>1</td>
</tr>
</table>
</body>
</html>



<h2 id="2-表单的验证"><a href="#2-表单的验证" class="headerlink" title="2.表单的验证"></a>2.表单的验证</h2><h3 id="2-1内置表单验证"><a href="#2-1内置表单验证" class="headerlink" title="2.1内置表单验证"></a>2.1内置表单验证</h3><p><code> wtforms.validators</code> 中有很多内置的表单验证</p>
<p>``validators.py` 源码中有很多定义好的验证方法</p>
<div class="code-wrapper"><pre class="line-numbers language-python" data-language="python"><code class="language-python">email <span class="token operator">=</span> Email
equal_to <span class="token operator">=</span> EqualTo
ip_address <span class="token operator">=</span> IPAddress
mac_address <span class="token operator">=</span> MacAddress
length <span class="token operator">=</span> Length
number_range <span class="token operator">=</span> NumberRange
optional <span class="token operator">=</span> Optional
required <span class="token operator">=</span> Required
input_required <span class="token operator">=</span> InputRequired
data_required <span class="token operator">=</span> DataRequired
regexp <span class="token operator">=</span> Regexp
url <span class="token operator">=</span> URL
any_of <span class="token operator">=</span> AnyOf
none_of <span class="token operator">=</span> NoneOf<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div>


<h3 id="2-2-自定义表单的验证"><a href="#2-2-自定义表单的验证" class="headerlink" title="2.2 自定义表单的验证"></a>2.2 自定义表单的验证</h3><h4 id="a-自定义当前表单的验证"><a href="#a-自定义当前表单的验证" class="headerlink" title="a.自定义当前表单的验证"></a>a.自定义当前表单的验证</h4><p><span style='color:red'>注意：</span></p>
<ul>
<li>自定义表单验证的函数名称 <code>validate_xxx</code>   <code>xxx</code>为要验证的字段名称</li>
</ul>
<div class="code-wrapper"><pre class="line-numbers language-python" data-language="python"><code class="language-python"><span class="token keyword">class</span> <span class="token class-name">RegisterForm</span><span class="token punctuation">(</span>FlaskForm<span class="token punctuation">)</span><span class="token punctuation">:</span>
    username <span class="token operator">=</span> StringField<span class="token punctuation">(</span>label<span class="token operator">=</span><span class="token string">'用户名'</span><span class="token punctuation">)</span>

    <span class="token keyword">def</span> <span class="token function">validate_username</span><span class="token punctuation">(</span>self<span class="token punctuation">,</span> field<span class="token punctuation">)</span><span class="token punctuation">:</span>
        <span class="token keyword">if</span> <span class="token builtin">len</span><span class="token punctuation">(</span>field<span class="token punctuation">.</span>data<span class="token punctuation">)</span> <span class="token operator">></span> <span class="token number">10</span><span class="token punctuation">:</span>
            <span class="token keyword">raise</span> ValidationError<span class="token punctuation">(</span><span class="token string">"用户名不能超过10个字符"</span><span class="token punctuation">)</span>
        <span class="token keyword">else</span><span class="token punctuation">:</span>
            <span class="token keyword">return</span> field<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div>
<p>模板展示错误信息  <code>form.xxx.errors</code> <class list>类型</p>
<div class="code-wrapper"><pre class="line-numbers language-html" data-language="html"><code class="language-html"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>p</span><span class="token punctuation">></span></span>
    &#123;&#123; register_form.username.label &#125;&#125;:
    &#123;&#123; register_form.username &#125;&#125;
    &#123;% if register_form.username.errors %&#125;
        &#123;% for i in register_form.username.errors %&#125;
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>span</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>error_text<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>&#123;&#123; i &#125;&#125;<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>span</span><span class="token punctuation">></span></span>
        &#123;% endfor %&#125;
    &#123;% endif %&#125;
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>p</span><span class="token punctuation">></span></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div>
<p><img src="http://qiniuyun.quancundexiwang.wang/20201215133108.png" srcset="/img/loading.gif" alt="image-20201215133108153"></p>
<h4 id="b-自定义全局的表单验证"><a href="#b-自定义全局的表单验证" class="headerlink" title="b.自定义全局的表单验证"></a>b.自定义全局的表单验证</h4><div class="code-wrapper"><pre class="line-numbers language-python" data-language="python"><code class="language-python"><span class="token keyword">def</span> <span class="token function">phone_required</span><span class="token punctuation">(</span>form<span class="token punctuation">,</span> field<span class="token punctuation">)</span><span class="token punctuation">:</span>
    phone_field <span class="token operator">=</span> field<span class="token punctuation">.</span>data
    <span class="token keyword">if</span> re<span class="token punctuation">.</span>search<span class="token punctuation">(</span><span class="token string">'^1[0-9]&#123;10&#125;$'</span><span class="token punctuation">,</span> phone_field<span class="token punctuation">)</span><span class="token punctuation">:</span>
        <span class="token keyword">return</span> field
    <span class="token keyword">else</span><span class="token punctuation">:</span>
        <span class="token keyword">raise</span> ValidationError<span class="token punctuation">(</span><span class="token string">"请输入手机号码"</span><span class="token punctuation">)</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div>
<div class="code-wrapper"><pre class="line-numbers language-python" data-language="python"><code class="language-python">
<span class="token keyword">class</span> <span class="token class-name">RegisterForm</span><span class="token punctuation">(</span>FlaskForm<span class="token punctuation">)</span><span class="token punctuation">:</span>
    username <span class="token operator">=</span> StringField<span class="token punctuation">(</span>label<span class="token operator">=</span><span class="token string">'用户名'</span><span class="token punctuation">,</span> validators<span class="token operator">=</span><span class="token punctuation">[</span>phone_required<span class="token punctuation">]</span><span class="token punctuation">)</span>
 		<span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre></div>
<p><img src="http://qiniuyun.quancundexiwang.wang/20201215135451.png" srcset="/img/loading.gif" alt="image-20201215135451320"></p>
<h2 id="3-文件上传"><a href="#3-文件上传" class="headerlink" title="3.文件上传"></a>3.文件上传</h2><h3 id="3-1-使用原生的方式上传"><a href="#3-1-使用原生的方式上传" class="headerlink" title="3.1 使用原生的方式上传"></a>3.1 使用原生的方式上传</h3><p><code>app.py</code></p>
<div class="code-wrapper"><pre class="line-numbers language-python" data-language="python"><code class="language-python"><span class="token decorator annotation punctuation">@app<span class="token punctuation">.</span>route</span><span class="token punctuation">(</span><span class="token string">'/upload'</span><span class="token punctuation">,</span> methods<span class="token operator">=</span><span class="token punctuation">[</span><span class="token string">'GET'</span><span class="token punctuation">,</span> <span class="token string">'POST'</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
<span class="token keyword">def</span> <span class="token function">upload</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span>

    <span class="token keyword">if</span> request<span class="token punctuation">.</span>method <span class="token operator">==</span> <span class="token string">'POST'</span><span class="token punctuation">:</span>
      
        <span class="token comment"># 获取当前文件的路径与assets文件夹拼接成，静态文件放至的文件夹路径</span>
			 	file_path <span class="token operator">=</span> os<span class="token punctuation">.</span>path<span class="token punctuation">.</span>join<span class="token punctuation">(</span>os<span class="token punctuation">.</span>path<span class="token punctuation">.</span>dirname<span class="token punctuation">(</span>__file__<span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token string">'assets'</span><span class="token punctuation">)</span>
        
        <span class="token comment"># 判断这个路径下的文件夹是否存在，不存在则新建`assets`文件夹</span>
    	 	<span class="token keyword">if</span> <span class="token keyword">not</span> os<span class="token punctuation">.</span>path<span class="token punctuation">.</span>exists<span class="token punctuation">(</span>file_path<span class="token punctuation">)</span><span class="token punctuation">:</span>
        	 os<span class="token punctuation">.</span>makedirs<span class="token punctuation">(</span>file_path<span class="token punctuation">)</span>
            
        <span class="token comment"># 获取file实例对象</span>
        <span class="token builtin">file</span> <span class="token operator">=</span> request<span class="token punctuation">.</span>files<span class="token punctuation">.</span>get<span class="token punctuation">(</span><span class="token string">'file1'</span><span class="token punctuation">,</span> <span class="token boolean">None</span><span class="token punctuation">)</span>

        <span class="token comment"># 拼接文件夹+filename生产文件的绝对路径</span>
        file_name <span class="token operator">=</span> os<span class="token punctuation">.</span>path<span class="token punctuation">.</span>join<span class="token punctuation">(</span>file_path<span class="token punctuation">,</span> <span class="token builtin">file</span><span class="token punctuation">.</span>filename<span class="token punctuation">)</span>

        <span class="token comment"># 执行file实例对象的save方法，根据文件绝对路径存入硬盘</span>
        <span class="token builtin">file</span><span class="token punctuation">.</span>save<span class="token punctuation">(</span>file_name<span class="token punctuation">)</span>

        <span class="token keyword">return</span> render_template<span class="token punctuation">(</span><span class="token string">'upload.html'</span><span class="token punctuation">,</span> src<span class="token operator">=</span>file_name<span class="token punctuation">)</span>

    <span class="token keyword">else</span><span class="token punctuation">:</span>
        <span class="token keyword">return</span> render_template<span class="token punctuation">(</span><span class="token string">'upload.html'</span><span class="token punctuation">)</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div>
<p><code>upload.html</code></p>
<div class="code-wrapper"><pre class="line-numbers language-html" data-language="html"><code class="language-html"><span class="token doctype"><span class="token punctuation">&lt;!</span><span class="token doctype-tag">DOCTYPE</span> <span class="token name">html</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>html</span> <span class="token attr-name">lang</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>en<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>head</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>meta</span> <span class="token attr-name">charset</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>UTF-8<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>title</span><span class="token punctuation">></span></span>Title<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>title</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>head</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>body</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>form</span> <span class="token attr-name">action</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>&#123;&#123; url_for(<span class="token punctuation">"</span></span><span class="token attr-name">upload")</span> <span class="token attr-name">&#125;&#125;"</span> <span class="token attr-name">method</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>post<span class="token punctuation">"</span></span> <span class="token attr-name">enctype</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>multipart/form-data<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>input</span> <span class="token attr-name">type</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>file<span class="token punctuation">"</span></span> <span class="token attr-name">name</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>file1<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>input</span> <span class="token attr-name">type</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>submit<span class="token punctuation">"</span></span> <span class="token attr-name">value</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>确定上传<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>form</span><span class="token punctuation">></span></span>
&#123;&#123; src &#125;&#125;
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>body</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>html</span><span class="token punctuation">></span></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div>
<p><img src="http://qiniuyun.quancundexiwang.wang/20201215152107.gif" srcset="/img/loading.gif" alt="Dec-15-2020 15-20-45"></p>
<p><img src="http://qiniuyun.quancundexiwang.wang/20201215152641.png" srcset="/img/loading.gif" alt="image-20201215152641782"></p>
<h3 id="3-2-上传文件文件名的安全获取"><a href="#3-2-上传文件文件名的安全获取" class="headerlink" title="3.2 上传文件文件名的安全获取"></a>3.2 上传文件文件名的安全获取</h3><blockquote>
<p>此函数获取文件名时安全，但对中文名获取时，获取不到中文，需要修改源码的编码<br>如不安全获取，直接file.filename</p>
</blockquote>
<p>比如这种文件名称</p>
<p><img src="http://qiniuyun.quancundexiwang.wang/20201215154935.png" srcset="/img/loading.gif" alt="image-20201215154935559"></p>
<p>上传完成之后 <img src="http://qiniuyun.quancundexiwang.wang/20201215154959.png" srcset="/img/loading.gif" alt="image-20201215154959137"></p>
<p>去浏览器访问</p>
<p><img src="http://qiniuyun.quancundexiwang.wang/20201215155026.png" srcset="/img/loading.gif" alt="image-20201215155025965"></p>
<p>所以要对文件的名称有安全措施，防止因为文件名导致的资源丢失</p>
<div class="code-wrapper"><pre class="line-numbers language-python" data-language="python"><code class="language-python"><span class="token keyword">from</span> werkzeug<span class="token punctuation">.</span>utils <span class="token keyword">import</span> secure_filename<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre></div>
<p>拼接 文件名称的时候</p>
<div class="code-wrapper"><pre class="line-numbers language-python" data-language="python"><code class="language-python"><span class="token comment"># 拼接文件夹+filename生产文件的绝对路径</span>

file_name <span class="token operator">=</span> os<span class="token punctuation">.</span>path<span class="token punctuation">.</span>join<span class="token punctuation">(</span>file_path<span class="token punctuation">,</span> secure_filename<span class="token punctuation">(</span><span class="token builtin">file</span><span class="token punctuation">.</span>filename<span class="token punctuation">)</span><span class="token punctuation">)</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre></div>
<p>Try Again !</p>
<p>再次上传那个文件，发现帮你帮文件名空格去掉了</p>
<p><img src="http://qiniuyun.quancundexiwang.wang/20201215155335.png" srcset="/img/loading.gif" alt="image-20201215155335373"></p>
<p>也可以正常访问</p>
<p><img src="http://qiniuyun.quancundexiwang.wang/20201215155408.png" srcset="/img/loading.gif" alt="image-20201215155408461"></p>
<h3 id="3-3-使用WTF-表单上传文件"><a href="#3-3-使用WTF-表单上传文件" class="headerlink" title="3.3 使用WTF 表单上传文件"></a>3.3 使用<code>WTF</code> 表单上传文件</h3><ul>
<li><code>FileField</code></li>
<li><code>FileRequired  </code> 必须上传</li>
<li><code>FileAllowed</code>  文件属性的验证 (类型，大小等)</li>
</ul>
<p><code>form.py</code></p>
<div class="code-wrapper"><pre class="line-numbers language-python" data-language="python"><code class="language-python"><span class="token keyword">from</span> flask_wtf <span class="token keyword">import</span> FlaskForm  <span class="token comment"># 引入表单模型的类</span>
<span class="token keyword">from</span> flask_wtf<span class="token punctuation">.</span><span class="token builtin">file</span> <span class="token keyword">import</span> FileRequired<span class="token punctuation">,</span> FileAllowed<span class="token punctuation">,</span> FileField

<span class="token keyword">class</span> <span class="token class-name">AvatarFileUpload</span><span class="token punctuation">(</span>FlaskForm<span class="token punctuation">)</span><span class="token punctuation">:</span>
    avatar <span class="token operator">=</span> FileField<span class="token punctuation">(</span>label<span class="token operator">=</span><span class="token string">'上传头像'</span><span class="token punctuation">,</span> validators<span class="token operator">=</span><span class="token punctuation">[</span>
        FileRequired<span class="token punctuation">(</span><span class="token string">"尚未上传头像文件"</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
        FileAllowed<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string">'jpg'</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token string">'只能上传jpg格式的图片'</span><span class="token punctuation">)</span>
    <span class="token punctuation">]</span><span class="token punctuation">)</span>
    submit <span class="token operator">=</span> SubmitField<span class="token punctuation">(</span><span class="token string">"提交"</span><span class="token punctuation">)</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div>
<p><code>app.py</code></p>
<div class="code-wrapper"><pre class="line-numbers language-python" data-language="python"><code class="language-python"><span class="token comment"># 自定义app里面的静态文件存储路径的配置，</span>

app<span class="token punctuation">.</span>config<span class="token punctuation">[</span>assets_file_storage<span class="token punctuation">]</span> <span class="token operator">=</span> os<span class="token punctuation">.</span>path<span class="token punctuation">.</span>join<span class="token punctuation">(</span>os<span class="token punctuation">.</span>path<span class="token punctuation">.</span>dirname<span class="token punctuation">(</span>__file__<span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token string">'assets'</span><span class="token punctuation">)</span>


<span class="token decorator annotation punctuation">@app<span class="token punctuation">.</span>route</span><span class="token punctuation">(</span><span class="token string">'/upload/avatar'</span><span class="token punctuation">,</span> methods<span class="token operator">=</span><span class="token punctuation">[</span><span class="token string">'GET'</span><span class="token punctuation">,</span> <span class="token string">'POST'</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
<span class="token keyword">def</span> <span class="token function">upload_avatar</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
    form <span class="token operator">=</span> AvatarFileUpload<span class="token punctuation">(</span><span class="token punctuation">)</span>
    <span class="token keyword">if</span> request<span class="token punctuation">.</span>method <span class="token operator">==</span> <span class="token string">'GET'</span><span class="token punctuation">:</span>
        <span class="token keyword">return</span> render_template<span class="token punctuation">(</span><span class="token string">'avatar.html'</span><span class="token punctuation">,</span> form<span class="token operator">=</span>form<span class="token punctuation">)</span>
    <span class="token keyword">else</span><span class="token punctuation">:</span>
        <span class="token keyword">print</span><span class="token punctuation">(</span>form<span class="token punctuation">.</span>avatar<span class="token punctuation">.</span>data<span class="token punctuation">)</span>
        <span class="token keyword">if</span> form<span class="token punctuation">.</span>validate_on_submit<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
            img_file <span class="token operator">=</span> form<span class="token punctuation">.</span>avatar<span class="token punctuation">.</span>data
            file_full_path <span class="token operator">=</span> os<span class="token punctuation">.</span>path<span class="token punctuation">.</span>join<span class="token punctuation">(</span>app<span class="token punctuation">.</span>config<span class="token punctuation">[</span>assets_file_storage<span class="token punctuation">]</span><span class="token punctuation">,</span> img_file<span class="token punctuation">.</span>filename<span class="token punctuation">)</span>
            img_file<span class="token punctuation">.</span>save<span class="token punctuation">(</span>file_full_path<span class="token punctuation">)</span>
            <span class="token keyword">return</span> render_template<span class="token punctuation">(</span><span class="token string">'avatar.html'</span><span class="token punctuation">,</span> form<span class="token operator">=</span>form<span class="token punctuation">,</span>src<span class="token operator">=</span>file_full_path<span class="token punctuation">)</span>
        <span class="token keyword">else</span><span class="token punctuation">:</span>
            <span class="token keyword">print</span><span class="token punctuation">(</span>form<span class="token punctuation">.</span>errors<span class="token punctuation">)</span>
        <span class="token keyword">return</span> render_template<span class="token punctuation">(</span><span class="token string">'avatar.html'</span><span class="token punctuation">,</span> form<span class="token operator">=</span>form<span class="token punctuation">)</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div>
<p><code>avatar.html</code></p>
<div class="code-wrapper"><pre class="line-numbers language-html" data-language="html"><code class="language-html"><span class="token doctype"><span class="token punctuation">&lt;!</span><span class="token doctype-tag">DOCTYPE</span> <span class="token name">html</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>html</span> <span class="token attr-name">lang</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>en<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>head</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>meta</span> <span class="token attr-name">charset</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>UTF-8<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>title</span><span class="token punctuation">></span></span>Title<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>title</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>head</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>body</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>form</span> <span class="token attr-name">action</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>&#123;&#123; url_for(<span class="token punctuation">'</span>upload_avatar<span class="token punctuation">'</span>) &#125;&#125;<span class="token punctuation">"</span></span> <span class="token attr-name">method</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>post<span class="token punctuation">"</span></span> <span class="token attr-name">enctype</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>multipart/form-data<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
    &#123;&#123; form.csrf_token &#125;&#125;
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>p</span><span class="token punctuation">></span></span>
        &#123;&#123; form.avatar.label &#125;&#125;
        &#123;&#123; form.avatar &#125;&#125;
        &#123;% if form.avatar.errors %&#125;
            &#123;&#123; form.avatar.errors &#125;&#125;
        &#123;% endif %&#125;
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>p</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>p</span><span class="token punctuation">></span></span>
        &#123;&#123; form.submit &#125;&#125;
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>p</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>p</span><span class="token punctuation">></span></span>&#123;&#123; src &#125;&#125;<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>p</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>form</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>body</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>html</span><span class="token punctuation">></span></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div>
<p><img src="http://qiniuyun.quancundexiwang.wang/20201215173721.gif" srcset="/img/loading.gif" alt="Dec-15-2020 17-36-26"></p>
<p><code>必填和文件类型的验证</code></p>
<p>不能只搞知识点！</p>
<p>flask + sqlalchemy + wtf + jinja2   python 项目搞起来</p>

            </div>
            <hr>
            <div>
              <div class="post-metas mb-3">
                
                  <div class="post-meta mr-3">
                    <i class="iconfont icon-category"></i>
                    
                      <a class="hover-with-bg" href="/categories/python/">python</a>
                    
                  </div>
                
                
                  <div class="post-meta">
                    <i class="iconfont icon-tags"></i>
                    
                      <a class="hover-with-bg" href="/tags/flask-form/">flask-form</a>
                    
                  </div>
                
              </div>
              
                <p class="note note-warning">本博客所有文章除特别声明外，均采用 <a target="_blank" href="https://creativecommons.org/licenses/by-sa/4.0/deed.zh" rel="nofollow noopener noopener">CC BY-SA 4.0 协议</a> ，转载请注明出处！</p>
              
              
                <div class="post-prevnext">
                  <article class="post-prev col-6">
                    
                    
                      <a href="/posts/46563/">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">正则表达式</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                      <a href="/posts/34916/">
                        <span class="hidden-mobile">工具-使用`Mitmproxy`抓包工具抓取移动端数据包</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </article>
                </div>
              
            </div>

            
              <!-- Comments -->
              <article class="comments" id="comments">
                
                
  <div id="vcomments"></div>
  <script type="text/javascript">
    Fluid.utils.waitElementVisible('vcomments', function() {
      Fluid.utils.createScript('https://cdn.jsdelivr.net/npm/valine@1.4.14/dist/Valine.min.js', function () {
        new Valine({
          el: "#vcomments",
          app_id: "Mi65hxq7VAFUDwOLeIGAOgiV-gzGzoHsz",
          app_key: "hMuhiD4FRqhns4giqLiEH9HG",
          placeholder: "说点什么",
          path: window.location.pathname,
          avatar: "retro",
          meta: ["nick","mail","link"],
          pageSize: "10",
          lang: "zh-CN",
          highlight: false,
          recordIP: false,
          serverURLs: "",
        });
      });
    });
  </script>
  <noscript>Please enable JavaScript to view the
    <a target="_blank" href="https://valine.js.org" rel="nofollow noopener noopener">comments powered by Valine.</a>
  </noscript>


              </article>
            
          </article>
        </div>
      </div>
    </div>
    
      <div class="d-none d-lg-block col-lg-2 toc-container" id="toc-ctn">
        <div id="toc">
  <p class="toc-header"><i class="iconfont icon-list"></i>&nbsp;目录</p>
  <div class="toc-body" id="toc-body"></div>
</div>

      </div>
    
  </div>
</div>

<!-- Custom -->


    

    
      <a id="scroll-top-button" href="#" role="button">
        <i class="iconfont icon-arrowup" aria-hidden="true"></i>
      </a>
    

    
      <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v"
                 for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>
    

    
  </main>

  <footer class="text-center mt-5 py-3">
  <div class="footer-content">
    

  </div>
  
  <div class="statistics">
    
    

    
      
        <!-- 不蒜子统计PV -->
        <span id="busuanzi_container_site_pv" style="display: none">
            总访问量 
            <span id="busuanzi_value_site_pv"></span>
             次
          </span>
      
      
        <!-- 不蒜子统计UV -->
        <span id="busuanzi_container_site_uv" style="display: none">
            总访客数 
            <span id="busuanzi_value_site_uv"></span>
             人
          </span>
      
    
  </div>


  
  <!-- 备案信息 -->
  <div class="beian">
    <span>
      <a href="http://beian.miit.gov.cn/" target="_blank" rel="nofollow noopener">
        豫ICP备20009912号-1
      </a>
    </span>
    
  </div>


  
</footer>

<!-- SCRIPTS -->

  <script  src="https://cdn.jsdelivr.net/npm/nprogress@0.2.0/nprogress.min.js" ></script>
  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/nprogress@0.2.0/nprogress.min.css" />

  <script>
    NProgress.configure({"showSpinner":true,"trickleSpeed":100})
    NProgress.start()
    window.addEventListener('load', function() {
      NProgress.done();
    })
  </script>


<script  src="https://cdn.jsdelivr.net/npm/jquery@3.5.1/dist/jquery.min.js" ></script>
<script  src="https://cdn.jsdelivr.net/npm/bootstrap@4.5.3/dist/js/bootstrap.min.js" ></script>
<script  src="/js/debouncer.js" ></script>
<script  src="/js/events.js" ></script>
<script  src="/js/plugins.js" ></script>

<!-- Plugins -->


  
    
      <script  src="/js/lazyload.js" ></script>
    
  



  
    
  



  <script  src="https://cdn.jsdelivr.net/npm/tocbot@4.12.0/dist/tocbot.min.js" ></script>



  <script  src="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3.5.7/dist/jquery.fancybox.min.js" ></script>



  <script  src="https://cdn.jsdelivr.net/npm/anchor-js@4.3.0/anchor.min.js" ></script>



  <script defer src="https://cdn.jsdelivr.net/npm/clipboard@2.0.6/dist/clipboard.min.js" ></script>



  <script defer src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js" ></script>




  <script  src="https://cdn.jsdelivr.net/npm/typed.js@2.0.11/lib/typed.min.js" ></script>
  <script>
    (function (window, document) {
      var typing = Fluid.plugins.typing;
      var title = document.getElementById('subtitle').title;
      
      typing(title)
      
    })(window, document);
  </script>



  <script  src="/js/local-search.js" ></script>
  <script>
    (function () {
      var path = "http://qiniuyun.quancundexiwang.wang/xmlSearch.xml";
      var inputArea = document.querySelector("#local-search-input");
      inputArea.onclick = function () {
        searchFunc(path, 'local-search-input', 'local-search-result');
        this.onclick = null
      }
    })()
  </script>















<!-- 主题的启动项 保持在最底部 -->
<script  src="/js/boot.js" ></script>



</body>
</html>
